package com.global.gbinventory.main.controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//import utils.DateConverter;


public class DBExecutor {
	
	private Connection connection;
	private Statement statement;
	
	public DBExecutor() throws ClassNotFoundException{
		//load the sqlite-JDBC driver using the current class loader
		Class.forName("org.sqlite.JDBC");
		
		try {
			connection = DriverManager.getConnection("jdbc:sqlite:global.db");
			statement = connection.createStatement();
			statement.setQueryTimeout(30); // set timeout to 30 sec
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws ClassNotFoundException {
		DBExecutor tc = new DBExecutor();
//		tc.createTables();
		tc.queryTable("Supplier");
		tc.closeConnection();
		
	}
	
	public void createTables(){
//		try{
			
//			statement.executeUpdate("drop table if exists Merek");
//			statement.executeUpdate("create table Merek (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"merek string)");
//			
//			statement.executeUpdate("drop table if exists Gudang");
//			statement.executeUpdate("create table Gudang (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"kode string, nama string, lokasi string)");
//			
//			statement.executeUpdate("drop table if exists Satuan");
//			statement.executeUpdate("create table Satuan (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"satuan string)");
//			
//			statement.executeUpdate("drop table if exists Pelanggan");
//			statement.executeUpdate("create table Pelanggan (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"kode string, nama string, alamat string, kota string, kodepos string, provinsi string, " +
//					"telepon string, hp string, email string, keterangan string)");
//			
//			statement.executeUpdate("drop table if exists Supplier");
//			statement.executeUpdate("create table Supplier (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"kodesupplier string, namasupplier string, alamat string, kota string, kodepos string, provinsi string, " +
//					"telepon string, namaagen string, hp string, email string, keterangan string)");
//			
//			statement.executeUpdate("drop table if exists Karyawan");
//			statement.executeUpdate("create table Karyawan (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"kode string, nama string, alamat string, kota string, kodepos string, provinsi string, " +
//					"telepon string, hp string, email string, keterangan string)");
			
//			statement.executeUpdate("drop table if exists Grup");
//			statement.executeUpdate("create table Grup (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"nama string)");

//			statement.executeUpdate("drop table if exists Subgrup");
//			statement.executeUpdate("create table Subgrup (id integer PRIMARY KEY AUTOINCREMENT, " +
//					"nama string, grup integer, FOREIGN KEY (grup) REFERENCES Grup(id) ON DELETE CASCADE)");
//			
//		} catch (SQLException e){
//			//if the error message is "out of memory",
//			//it probably means no database file is found
//			System.err.println(e.getMessage());
//		} 
	}
	
	public void queryTable(String tableName){
		System.out.println("========== Table " + tableName + " ==========");
		String sql = "Select * from " + tableName;
		try {
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
//				System.out.println(rs.getObject(2) + "   " + rs.getObject(3) + "   " + rs.getObject(4) + "   " 
//						+ rs.getObject(5) + "   " + rs.getObject(6) + "   " + rs.getObject(7) + "   " + rs.getObject(8) +
//						"   " + rs.getObject(9) + "   " + rs.getObject(10) + "   " + rs.getObject(11));
				
				System.out.println(rs.getObject(1) + "   " + rs.getObject(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("========== End Table ==========");
	}
	
	public void closeConnection(){
		try{
			if(connection != null){
				connection.close();
			}
		} catch(SQLException e){
			//connection close failed
			System.err.println(e);
		}
	}
	
}
